﻿using System;
using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DA_ServiceTax
/// </summary>
public class DA_ServiceTax
{
    AuditBillingEntities objABEnt;
    public DA_ServiceTax()
    {
        //
        // TODO: Add constructor logic here
        //
        objABEnt = new AuditBillingEntities();
    }

    public List<PLServiceTax> DA_fnGetServiceTaxList(PLServiceTax pObjServ)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from s in objABEnt.MstServiceTaxes
                          where (pObjServ.SearchIsActive == null || s.IsActive == pObjServ.SearchIsActive)
                          select new PLServiceTax()
                          {
                              ServiceTaxID = s.MstServiceTaxID,
                              ServiceTaxStartDate = s.ServiceTaxEffStartDate,
                              ServiceTaxEndDate = s.ServiceTaxEffEndDate,
                              ServiceTaxPer = s.ServiceTaxPercentage,
                              IsActive = s.IsActive
                          });
            return varRes.ToList();
        }
    }

    public int DA_InsertUpdateServiceTax(PLServiceTax pObjFinYear)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from s in objABEnt.MstServiceTaxes
                          where s.MstServiceTaxID != pObjFinYear.ServiceTaxID &&
                          ((pObjFinYear.ServiceTaxStartDate >= s.ServiceTaxEffStartDate && pObjFinYear.ServiceTaxStartDate <= s.ServiceTaxEffEndDate) ||
                          (pObjFinYear.ServiceTaxEndDate >= s.ServiceTaxEffStartDate && pObjFinYear.ServiceTaxEndDate <= s.ServiceTaxEffEndDate))
                          select s);
            if (varRes.Count() > 0)
                return -1;
            else
            {
                MstServiceTax objMst = new MstServiceTax();
                if (pObjFinYear.ServiceTaxID != 0)
                    objMst = objABEnt.MstServiceTaxes.Where(s => s.MstServiceTaxID == pObjFinYear.ServiceTaxID).FirstOrDefault();
                else
                    objMst = new MstServiceTax();

                objMst.ServiceTaxEffStartDate = pObjFinYear.ServiceTaxStartDate;
                objMst.ServiceTaxEffEndDate = pObjFinYear.ServiceTaxEndDate;
                objMst.ServiceTaxPercentage = pObjFinYear.ServiceTaxPer;
                objMst.IsActive = pObjFinYear.IsActive;

                if (pObjFinYear.ServiceTaxID != 0)
                {
                    objMst.ModifiedBy = SessionInfo.prop_getObj.EmployeeID;
                    objMst.ModifiedOn = DateTime.Now;
                }
                else
                {
                    objMst.CreatedBy = SessionInfo.prop_getObj.EmployeeID;
                    objMst.CreatedOn = DateTime.Now;
                    objABEnt.MstServiceTaxes.Add(objMst);
                }
                objABEnt.SaveChanges();
                return 1;
            }

        }
    }

    public List<PLServiceTax> DA_fnGetServiceTaxDetailList(PLServiceTax pObjServ)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from s in objABEnt.MstServiceTaxDetails
                          join t in objABEnt.MstServiceTaxes on s.MstServiceTaxID equals t.MstServiceTaxID
                          where (pObjServ.SearchIsActive == null || s.IsActive == pObjServ.SearchIsActive) &&
                          (pObjServ.SearchServiceTaxID == null || s.MstServiceTaxID == pObjServ.SearchServiceTaxID)
                          orderby t.ServiceTaxEffStartDate, s.SortOrder
                          select new PLServiceTax()
                          {
                              ServiceTaxDetailsID = s.MstServiceTaxDetailID,
                              ServiceTaxID = (s.MstServiceTaxID ?? 0),
                              ServiceTaxMasterName = SqlFunctions.DateName("month", t.ServiceTaxEffStartDate).Substring(0, 3) + "-" + SqlFunctions.DateName("yy", t.ServiceTaxEffStartDate) + " " + SqlFunctions.DateName("month", t.ServiceTaxEffEndDate).Substring(0, 3) + "-" + SqlFunctions.DateName("yy", t.ServiceTaxEffEndDate),
                              ServiceTaxName = s.ServiceTaxName,
                              ServiceTaxPer = s.ServiceTaxPer,
                              SortOrder = s.SortOrder,
                              IsActive = s.IsActive
                          });
            return varRes.ToList();
        }
    }

    public List<PLServiceTax> DA_fnGetServiceTaxBinding()
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from s in objABEnt.MstServiceTaxes
                          where s.IsActive == true
                          select new PLServiceTax()
                          {
                              ServiceTaxID = s.MstServiceTaxID,
                              ServiceTaxName = SqlFunctions.DateName("month", s.ServiceTaxEffStartDate).Substring(0, 3) + "-" + SqlFunctions.DateName("yy", s.ServiceTaxEffStartDate) + " " + SqlFunctions.DateName("month", s.ServiceTaxEffEndDate).Substring(0, 3) + "-" + SqlFunctions.DateName("yy", s.ServiceTaxEffEndDate),
                          });
            return varRes.ToList();
        }
    }

    public int DA_InsertUpdateServiceTaxDetails(PLServiceTax pObjFinYear)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from s in objABEnt.MstServiceTaxDetails
                          where s.MstServiceTaxDetailID != pObjFinYear.MstServiceTaxDetailID &&
                          s.MstServiceTaxID == pObjFinYear.ServiceTaxID &&
                          s.ServiceTaxName == pObjFinYear.ServiceTaxName
                          select s);
            if (varRes.Count() > 0)
                return -1;
            else
            {
                MstServiceTaxDetail objMst = new MstServiceTaxDetail();
                if (pObjFinYear.MstServiceTaxDetailID != 0)
                    objMst = objABEnt.MstServiceTaxDetails.Where(s => s.MstServiceTaxDetailID == pObjFinYear.MstServiceTaxDetailID).FirstOrDefault();
                else
                    objMst = new MstServiceTaxDetail();

                objMst.MstServiceTaxID = pObjFinYear.ServiceTaxID;
                objMst.ServiceTaxName = pObjFinYear.ServiceTaxName;
                objMst.ServiceTaxPer = pObjFinYear.ServiceTaxPer;
                objMst.SortOrder = pObjFinYear.SortOrder;
                objMst.IsActive = pObjFinYear.IsActive;

                if (pObjFinYear.MstServiceTaxDetailID != 0)
                {
                    objMst.ModifiedBy = SessionInfo.prop_getObj.EmployeeID;
                    objMst.ModifiedOn = DateTime.Now;
                }
                else
                {
                    objMst.CreatedBy = SessionInfo.prop_getObj.EmployeeID;
                    objMst.CreatedOn = DateTime.Now;
                    objABEnt.MstServiceTaxDetails.Add(objMst);
                }
                objABEnt.SaveChanges();
                return 1;
            }

        }
    }

}